package com.iking.toolkit.ocr.controller;

import com.iking.toolkit.model.Msg;
import com.iking.toolkit.ocr.model.dto.ExtractUrlTextDTO;
import com.iking.toolkit.ocr.service.OcrService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 功能描述:ocr识别管理
 *
 * @author: yfl
 * @date: 2025/8/1 10:09
 */
@Slf4j
@Tag(name = "OcrController", description = "OCR识别管理")
@RequestMapping("/ocr")
@RestController
@Validated
public class OcrController {

    @Autowired
    private OcrService ocrService;


    @Operation(summary = "ocr识别图片中的文字")
    @PostMapping(value = "/recognizeImage", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Msg ocrRecognizeImage(@RequestParam("file")  MultipartFile multipartFile) {
        // 手动校验文件内容是否为空（避免空文件）
        if (multipartFile.isEmpty()) {
            return Msg.fail("上传的图片文件为空，请重新上传!");
        }
        // 判断文件类型是否为图片
        if (!multipartFile.getContentType().startsWith("image")) {
            return Msg.fail("请上传图片类型文件!");
        }
        return ocrService.ocrRecognizeImage(multipartFile);
    }
    @Operation(summary = "ocr识别pdf中的文字,上传文件")
    @PostMapping(value = "/extractPdfText", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Msg extractPdfText(@RequestParam("file")  MultipartFile multipartFile) {
        // 手动校验文件内容是否为空（避免空文件）
        if (multipartFile.isEmpty()) {
            return Msg.fail("上传的图片文件为空，请重新上传!");
        }
        // 判断文件类型是否为pdf
        if (!multipartFile.getContentType().startsWith("application/pdf")) {
            return Msg.fail("请上传pdf类型文件!");
        }
        return ocrService.extractPdfText(multipartFile);
    }
    @Operation(summary = "ocr识别pdf中的文字,url传参")
    @PostMapping("/extractUrlText")
    public Msg extractUrlText(@RequestBody @Valid ExtractUrlTextDTO extractUrlTextDTO) {
        return ocrService.extractUrlText(extractUrlTextDTO);
    }

}
